package 
{
  import com.bit101.components.FPSMeter;
  import com.bit101.components.Label;
  import com.bit101.components.PushButton;
	import flash.display.Sprite;
  import flash.display.StageScaleMode;
	import flash.events.Event;
  import flash.events.TimerEvent;
  import flash.text.StyleSheet;
  import flash.utils.Timer;
  import mge.ui.ModalDialog;

	/**
	 * ideas:
   * 
   * Cities:
   * Some cities generates money at a constant rate (note: bad for consistent score).
   * 
   * Towers:
   * Defense Tower: drop missiles to enemies walking nearby.
   * Cities can be upgraded to city-towers.
   * Invisibility Tower: Allow the friendly troops that visit this tower to become invisible for a while. Invisible troops can't be attacked by defense towers.
   * Speed tower.
   * Extra Strenght/Heal tower.
   * 
   * Gold
   * Gold mines provide a limited ammount of gold like in AOE.
   * 
   * Invading forces:
   * Like in a tower defense, external neutral enemy units come from map boundaries and attacks player or random cities, making more difficult to conquer the current map.
   * 
	 * @author Ivan
	 */
	[Frame(factoryClass="Preloader")]
	public class GameBoard extends Sprite 
	{

    public static const NEUTRAL_COLOR:int = 0xAAAAAA;
    
    public static const WIDTH:int = 800;
    public static const HEIGHT:int = 600;
    
		public function GameBoard():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

    private var debugPanelCreated:Boolean;
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
      stage.scaleMode = StageScaleMode.SHOW_ALL;
      
      // Creates players
      players = new Vector.<Player>();
      
      // AI updater
      enemyTimer = new Timer(1000);
      enemyTimer.addEventListener(TimerEvent.TIMER, stupidAIController);
      enemyTimer.start();
      
      if (!debugPanelCreated)
      {
        stage.addChild(new DebugPanel(this));
        debugPanelCreated = true;
      }
      
      new FPSMeter(this, 750);
		}
    
    // @ skillAI only for non-human players.
    public function createPlayer(player:Player, initialTown:Town):void
    {
      players.push(player);
      
      initialTown.owner = player;
      initialTown.population = 30;
      initialTown.populationLimit = 50;
      
      var playerInfoPanel:PlayerInfoPanel = new PlayerInfoPanel(player);
      playerInfoPanel.x = players.length * playerInfoPanel.width * 2 + 10;
      playerInfoPanel.y = HEIGHT - playerInfoPanel.height - 10;
      addChild(playerInfoPanel);
      
    }
    
    // Distribuye towers en el mapa
    public function createTowns(townNumber:int):void
    {
      var columns:int = 12;
      var rows:int = 8;
      var gridSpace:Number = WIDTH / columns;
      
      towns = new Vector.<Town>();
      
      var townMap:Vector.<Boolean> = new Vector.<Boolean>(columns * rows);
      if (townNumber > townMap.length) townNumber = townMap.length;
      for (var i:int = 0; i < townMap.length; i++)
        townMap[i] = i < townNumber;
        
      townMap = randomizeVector(townMap);
      
      for (i = 0; i < townMap.length; i++)
      {
        if (townMap[i])
        {
          var t:Town = new Town();
          t.x = 50 + (int)(i % columns) * gridSpace;
          t.y = 50 + (int)(i / columns) * gridSpace;
          t.population = 0;
          t.populationLimit = 10 + Math.random() * 40;
          towns.push(t);
          addChild(t);
        }
      }
    }
       
    
    
    private function stupidAIController(e:Event):void
    {
      for (var i:int = 0; i < players.length; i++)
      {
        players[i].update();
        if (players[i].controller)
          players[i].controller.command();
      }
    }
    
    public function restart(result:*):void
    {
      while (this.numChildren > 0)
        removeChildAt(0);
      enemyTimer.removeEventListener(TimerEvent.TIMER, stupidAIController);
      init();
    }
    
    private function randomizeVector(v:Vector.<Boolean>):Vector.<Boolean>
    {
      var ans:Vector.<Boolean> = new Vector.<Boolean>();
      while (v.length > 0)
        ans.push(v.splice(Math.round(Math.random() * (v.length - 1)), 1)[0]);
      return ans;
    }
    
    public static var towns:Vector.<Town>;
    
    private var enemyTimer:Timer;
    private var players:Vector.<Player>;
	}

}